package com.lgkj.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.TextView;

import com.lgkj.R;
import com.lgkj.bean.CityShopBean;
import com.lgkj.ui.AdTextActivity;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by Administrator on 2018/8/10 0010.
 */

public class ShopNameAdapter extends BaseAdapter implements View.OnClickListener {

    private Context context;
    private List<CityShopBean.DataBean.BarbershopListBean> dataBeans;
    private MyClickListener mListener;

    public void setDataBeans(List<CityShopBean.DataBean.BarbershopListBean> dataBeans) {
        if (dataBeans != null) {
            this.dataBeans = dataBeans;
        }
    }

    private int type;

    public void setType(int type) {
        this.type = type;
    }

    public void setClick(MyClickListener listener) {
        this.mListener = listener;
    }

    //自定义接口，用于回调按钮点击事件到Activity
    public interface MyClickListener {
        void clickListener(View v);
    }

    public ShopNameAdapter(Context context) {
        this.context = context;
        this.dataBeans = new ArrayList<>();
    }

    @Override
    public int getCount() {
        return dataBeans.size();
    }

    @Override
    public CityShopBean.DataBean.BarbershopListBean getItem(int i) {
        return dataBeans.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        ViewHolder viewHolder;
        if (view == null) {
            view = LayoutInflater.from(context).inflate(R.layout.item_shop_name, null);
            viewHolder = new ViewHolder(view);
            view.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) view.getTag();
        }
        if (type == 0) {
            viewHolder.rbId.setChecked(dataBeans.get(i).getIscheck());
        } else if (type == 1) {
            viewHolder.rbId.setChecked(true);
            dataBeans.get(i).setIscheck(true);
        } else {
            dataBeans.get(i).setIscheck(false);
            viewHolder.rbId.setChecked(false);
        }
        if (dataBeans.get(i).getIscheck()) {
            AdTextActivity.counts.put(dataBeans.get(i).getId(), dataBeans.get(i).getProduct_count());
        } else {
            AdTextActivity.counts.remove(dataBeans.get(i).getId());
        }
        viewHolder.tvName.setText(dataBeans.get(i).getBarbershop_name() + "  (共" + dataBeans.get(i).getProduct_count() + "台)");
        viewHolder.rbId.setOnClickListener(this);
        viewHolder.rbId.setTag(i);
        return view;
    }

    @Override
    public void onClick(View view) {
        mListener.clickListener(view);
    }

    static class ViewHolder {
        @BindView(R.id.tv_name)
        TextView tvName;
        @BindView(R.id.rb_id)
        CheckBox rbId;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }

}
